package com.energy.analysis.sys.controller;

import com.energy.analysis.basic.entity.CollectData;
import com.energy.analysis.basic.entity.PriceData;
import com.energy.analysis.basic.entity.StandData;
import com.energy.analysis.basic.entity.StationData;
import com.energy.analysis.basic.service.CollectService;
import com.energy.analysis.basic.service.PriceService;
import com.energy.analysis.basic.service.StandService;
import com.energy.analysis.basic.service.StationService;
import com.energy.analysis.comm.base.BaseController;
import com.energy.analysis.comm.utils.DateUtils;
import com.energy.analysis.comm.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("rawtypes")
@Controller
public class IndexController extends BaseController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private StationService stationService;

    @Autowired
    private StandService standService;

    @Autowired
    private PriceService priceService;

    @RequestMapping("/")
    public ModelAndView index(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("base/main");
        pageInitToSession(session);
        modelAndView.addObject("menus", getMenus());
        modelAndView.addObject("content", "home");
        return modelAndView;
    }

    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("main");
        pageInitToSession(session);
        /* 初始化页面菜单数据 data */
        /* 这里取一下主页上需要的信息 **/
        //取一下机组信息
        List<StandData> standDataList = standService.getStandData();
        Float ft3q = 0f;//水
        Float jqi = 0f;//电
        Float qqi = 0f;//热

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("eTime", DateUtils.getTodayNow());
        map.put("groupType", 2);//接机组分组汇总
        List<CollectData> collectDataList = collectService.getCollectDate(map);
        for (CollectData collectData : collectDataList) {
            ft3q += collectData.getFt3q();
            jqi += collectData.getJqi();
            qqi += collectData.getQqi();
        }

        PriceData priceData = priceService.getPriceData();

        Float totalquantity = ft3q + jqi + qqi;//水、电、热总数据
        Float totalAmount = ft3q * priceData.getFt3qPrice() + jqi * priceData.getJqiPrice() + qqi * priceData.getQqiPrice();//水、电、热总金额

        //取一下昨天的数据
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("eTime", DateUtils.getTodayBegin());
        List<CollectData> collectDataYesList = collectService.getCollectDate(map1);
        Float yesFt3q = 0f;
        Float yesJqi = 0f;
        Float yesQqi = 0f;//昨日热量
        for (CollectData collectData : collectDataYesList) {
            yesFt3q = collectData.getFt3q();
            yesJqi = collectData.getJqi();
            yesQqi += collectData.getQqi();

        }

        //取一下换热站信息
        List<StationData> stationDataList = stationService.getStationData();

        int stationSize = 0;//换热站数量
        if (stationDataList != null && stationDataList.size() != 0) {
            stationSize = stationDataList.size();
        }
        int standArea = 0;
        for (StandData standData : standDataList) {
            standArea += standData.getRealArea();
        }

        modelAndView.addObject("ft3q", ft3q);
        modelAndView.addObject("jqi", jqi);
        modelAndView.addObject("qqi", qqi);
        modelAndView.addObject("yesFt3q", yesFt3q);
        modelAndView.addObject("yesJqi", yesJqi);
        modelAndView.addObject("yesQqi", yesQqi);
        modelAndView.addObject("priceData", priceData);
        modelAndView.addObject("collectDataList ", collectDataList);//基础数据
        modelAndView.addObject("totalquantity", totalquantity);//这是水、电、热总的数量
        modelAndView.addObject("totalAmount", totalAmount);//这是水、电、热总的金额
        modelAndView.addObject("standDataList", standDataList);//机组信息
        modelAndView.addObject("stationSize", stationSize);//换热站数量
        modelAndView.addObject("standArea", standArea);//机组总面积
        return modelAndView;
    }

    @RequestMapping("/welcome")
    public String welcome() {
        return "welcome";
    }

    /**
     * 密码修改
     *
     * @return
     */
    @RequestMapping("/up/passwd")
    @ResponseBody
    public Map<String, Object> upPassWd(String oldPassword, String newPassword) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        if (StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)) {
            retMap.put("code", "01");
            retMap.put("message", "参数错误");
        }
		/*else if(oprInfoService.modifyPassword(oldPassword,newPassword)){
			retMap.put("code", "00");
			retMap.put("message", "修改成功");
		}else{
			retMap.put("code", "02");
			retMap.put("message", "原密码错误");
		}*/
        return retMap;
    }
}